<route lang="yaml">
meta:
  requiresAuth: true
</route>

<template>
  <el-table :data="items" style="width: 100%">
    <el-table-column prop="name" label="求购名称" width="100" />
    <el-table-column prop="createdAt" label="创建于" width="200" />
    <el-table-column prop="price" label="心理价格" width="100" />
    <el-table-column prop="status" label="状态" width="100" />
    <el-table-column label="Operations" width="150">
      <template #default="scope">
        <el-button @click="deleteditem(scope.row.id)" size="small">
          删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import {
  getUserProfile,
  getpublishItem,
  User,
  Usermsg as msg,
  ItemPublish
} from '../../services/auth'
import { deleteItemById } from '../../services/items'
import { useRouter } from 'vue-router'
const router = useRouter()
const Usermsg = ref<User>({
  id: '',
  mobile: '',
  roles: [],
  createdAt: '',
  updatedAt: '',
  profile: {
    id: '',
    createdAt: '',
    updatedAt: '',
    nickname: '',
    avatar: '',
    gender: ''
  }
})
const items = ref<ItemPublish[]>()
onMounted(async () => {
  try {
    Usermsg.value = (await getUserProfile()).data
    const result = (await getpublishItem(Usermsg.value.id)).data
    result.items = result.items.filter((element: { status: string }) => {
      return element.status === 'REQUEST'
    })
    items.value = result.items
  } catch (err) {}
})

async function deleteditem(id: string) {
  const yn = (await deleteItemById(id)).data
  const result = (await getpublishItem(Usermsg.value.id)).data
  items.value = result.items
}
</script>
